<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input @input="handleQuery" v-model="queryParams.enterpriseName" placeholder="请输入企业名称" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="简称" prop="simpleName">
        <el-input @input="handleQuery" v-model="queryParams.simpleName" placeholder="请输入简称" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="法人代表" prop="legalRepresentative">
        <el-input @input="handleQuery" v-model="queryParams.legalRepresentative" placeholder="请输入法人代表" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input @input="handleQuery" v-model="queryParams.contactPerson" placeholder="请输入联系人" clearable
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MAINTENANCE_STATUS)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')"
          v-hasPermi="['business:maintenance-information:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="warning" plain @click="handleImport" v-hasPermi="['business:equipment-manage:import']">
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['business:maintenance-information:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['business:maintenance-information:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" v-loading="loading" :data="list" border :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="企业名称" align="center" min-width="200">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openViewDialog(row.id)">
            {{ row.enterpriseName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="简称" align="center" prop="simpleName" min-width="200" />
      <el-table-column label="信用代码" align="center" prop="creditCode" min-width="200" />
      <el-table-column label="企业代号" align="center" prop="enterpriseCode" min-width="200" />
      <el-table-column label="法人代表" align="center" prop="legalRepresentative" min-width="200" />
      <el-table-column label="公司官网" align="center" prop="companyWebsite" min-width="200" />
      <el-table-column label="联系人" align="center" prop="contactPerson" min-width="200" />
      <el-table-column label="联系电话" align="center" prop="mobilePhone" min-width="200" />
      <el-table-column label="固定电话" align="center" prop="landlinePhone" min-width="200" />
      <el-table-column label="传真号码" align="center" prop="faxNumber" min-width="200" />
      <el-table-column label="企业邮箱" align="center" prop="email" min-width="200" />
      <el-table-column label="入选时间" align="center" prop="entryDate" min-width="200" />
      <el-table-column label="状态" align="center" prop="status" min-width="200">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MAINTENANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="开户行" align="center" prop="bankName" min-width="200" />
      <el-table-column label="账号" align="center" prop="bankAccount" min-width="200" />
      <el-table-column label="税号" align="center" prop="taxNumber" min-width="200" />
      <el-table-column label="发票抬头" align="center" prop="invoiceTitle" min-width="200" />
      <el-table-column label="注册地址" align="center" prop="registeredAddress" min-width="200" />
      <el-table-column label="公司地址" align="center" prop="companyAddress" min-width="200" />
      <el-table-column label="经营范围" align="center" prop="businessScope" min-width="200" />
      <el-table-column label="企业信息" align="center" prop="enterpriseInformation" min-width="200">
        <template #default="scope">
          <div class="rich-text-content" v-html="scope.row.enterpriseInformation"></div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px"
        min-width="200" />
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:maintenance-information:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:maintenance-information:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MaintenanceInformationForm ref="formRef" @success="getList" />

  <!-- 查看 -->
  <MaintenanceView ref="viewRef" />


  <!-- 导入弹窗 -->
  <MaintenanceInfoImpotForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { MaintenanceInformationApi, MaintenanceInformation } from '@/api/business/maintenanceinformation'
import MaintenanceInformationForm from './MaintenanceInformationForm.vue'
import MaintenanceView from './MaintenanceInformationView.vue'
import MaintenanceInfoImpotForm from './MaintenanceInfoImportForm.vue'

const viewRef = ref()

/** 维保商档案 列表 */
defineOptions({ name: 'MaintenanceInformation' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<MaintenanceInformation[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  enterpriseName: undefined,
  simpleName: undefined,
  legalRepresentative: undefined,
  contactPerson: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MaintenanceInformationApi.getMaintenanceInformationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MaintenanceInformationApi.deleteMaintenanceInformation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除维保商档案 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await MaintenanceInformationApi.deleteMaintenanceInformationList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: MaintenanceInformation[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MaintenanceInformationApi.exportMaintenanceInformation(queryParams)
    download.excel(data, '维保商档案.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id)
}

/** 维保商信息导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
